package com.lanyun.controller;

import com.lanyun.pojo.Dept;
import com.lanyun.pojo.Emp;
import com.lanyun.service.DeptService;
import com.lanyun.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("/getAll")
    public String getAll(Model model){
        System.out.println("123");
        List<Emp> emps = empService.getAll();
       List<Dept> depts= deptService.getAll();

        model.addAttribute("count",emps.size());
        model.addAttribute("emps",emps);
        model.addAttribute("depts",depts);
      /*  for (int i = 0; i <emps.size() ; i++) {
            System.out.println(emps.get(i)==null);
            System.out.println(emps.get(i).getDept().getName());
        }*/
        return "Emp";
    }
    @RequestMapping("/getAll1")
    @ResponseBody
    public Object getAll1(){
        List<Emp> empList = empService.getAll();

        return empList;
    }

    @RequestMapping("/changeDept")
    @ResponseBody
    public Object changeDept(Integer id){
        List<Emp> emps = empService.findEmpById(id);
        return emps;
    }
    @RequestMapping("/addEmp")
    public String addEmp(Emp emp){//添加员工
        System.out.println("要增加的员工属性：  "+emp);
        Integer result = empService.insertEmp(emp);
        if (result==1){
            return "redirect:/getAll";
        }
        return "redirect:/getAll";
    }
    @RequestMapping("/del1")
    @ResponseBody
    public Object del(@RequestParam("eid")Integer id){//单项删除员工，点击删除按钮，拿到对应的id进行删除
        System.out.println("单项删除员工拿到的id：  "+id);
        Integer result = empService.deleteEmp(id);
        if (result==1){
            return result;
        }
        return "redirect:/getAll";
    }


    @RequestMapping("/delSome1")
    @ResponseBody
    public Object delSome(String ids){//批量删除--拿到选中的CheckBox--id进行批量删除
        System.out.println("批量删除拿到的id： ---  "+ids);
        Integer result = empService.deleteSomeEmp(ids);
        System.out.println("---------"+result);
        if (result>=1){
            return result;
        }else {
            return "redirect:/getAll";
        }
    }
    @RequestMapping("/updateEmp")
    @ResponseBody
    public Object updateEmp(@RequestParam("eid") Integer eid){//查找当前员工的所有信息，数据回显，修改功能
        System.out.println("修改员工拿到的id:  -- "+eid);
        Emp emp= empService.findEmp_update(eid);

        return emp;
    }
    @RequestMapping("/updateEmpReal")
    public String updateEmpReal(Emp emp){//拿到修改后的员工信息，进行修改操作
        System.out.println("修改后的员工信息：  --  "+emp);
        Integer result =empService.updateEmp(emp);
        System.out.println(result+"========================");
        return "redirect:/getAll";
    }

}